import { Component, OnInit } from '@angular/core';
import { JsonApiService } from "app/core/api/json-api.service"; //http请求配置引入
import { config } from '../../shared/smartadmin.config';

declare var $: any;
@Component({
	selector: 'sa-sisaiData',
	templateUrl: './sisaiData.component.html',
})
export class SisaiDataComponent implements OnInit {
	
	tableData: any[] = []; //table数据
	totalCount: number = 0; //分页总条数
	curPage: number = 1; //分页当前页
	pageSize: number = 10; //内置的分页每页条数
	searchKey: string = ''; //搜索keyword
	tableHeardlist=[
		{keys:'carNumber',value:'车牌号码 '},
		{keys:'weight',value:'单桶重量'},
		{keys:'uploadDate',value:'时间'},
		{keys:'address',value:'地址'},
	];
	tableQueryitem={
		startTime:'', 	//开始时间
		endTime:  '', 	//结束时间
		companyId: '',
		companyName: '',
		deviceId: '', 	//设备ID
		carNumber:  '',
		searchKey:'', 	//搜索keyword
	}
	constructor(private jsonApiService: JsonApiService) {}
	getSelectTableTime(time){
		this.tableQueryitem.startTime=time.startTime;
		this.tableQueryitem.endTime=time.endTime;
	}
	
	getSelectCarInfo(val){
		this.tableQueryitem.deviceId=val.deviceId;
		this.tableQueryitem.carNumber=val.carNumber;
		this.tableQueryitem.companyId=val.companyId;
		this.tableQueryitem.companyName=val.companyName;
	}
	ngOnInit() {
		this.load()
	}
	
	//导出报表
	export_totle() {
		var parameters = {
			deviceId: this.tableQueryitem.deviceId,
			startTime: this.tableQueryitem.startTime,
			endTime: this.tableQueryitem.endTime,
		}
		let timeCheck = common.correctTime(parameters)
		if(!timeCheck.status){
			this.jsonApiService.message(timeCheck.message);
			return;
		}
		common.exprotDataFile(parameters, 'param', config.BaseURL + 'weightStatistics/exportWeightDataStatistics.html')
	}
	//时间转化
	getTime(date) {
		var year = date.getFullYear();
		var month = (date.getMonth() + 1) < 10 ? "0" + (date.getMonth() + 1) : (date.getMonth() + 1);
		var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();

		return year + "-" + month + "-" + day;
	}
	//初始化加载list数据
	load() {
		let timeCheck=common.correctTimeParams(this.tableQueryitem.startTime,this.tableQueryitem.endTime,0);
		if(!timeCheck.status){
			this.jsonApiService.message(timeCheck.message);
			return;
		}
		let data = {
			pageSize:this.pageSize,
			nowPage:this.curPage,
			parameters:{
				deviceId:this.tableQueryitem.deviceId,
//				companyId:this.tableQueryitem.companyId,
				startTime:this.tableQueryitem.startTime,
				endTime:this.tableQueryitem.endTime,
				searchKey:this.tableQueryitem.searchKey,
			}
		}
		let reqdata = 'gridPager='+JSON.stringify(data);
		this.jsonApiService.http("weightStatistics/list.html", reqdata).subscribe(result => {
			if(!result.isSuccess){
				this.jsonApiService.message(result.message);
				return;
			}
//			for(let item of result.exhibitDatas){
//				item.weight=common.twoDecimal(item.weight);
//			}
			this.tableData = result.exhibitDatas;
			this.totalCount = result.recordCount;
		})
	}
	//刷新
    refresh(){
    	this.load();
    }
	//查询
	table_search(){
		this.curPage=1;
		this.searchKey = common.stringTrim(this.searchKey);
		this.load();
	}
	//分页回调
	paginate(e){
		this.curPage = e.page+1;
		this.pageSize = e.rows;
		this.load();
	}
	ngOnDestroy() {
		document.onclick = null;
	}



	//统计弹窗
	censusData: any={
		totalWeight: '',
		num: '',
		maxWeight: '',
		minWeight: ''
	}; //统计数据
	lgModalhide(modal){
		modal.hide();
	}
	census(modal){
		if(!this.tableQueryitem.deviceId){
			this.jsonApiService.message('请先选择车牌号码');
			return
		}
		if(!this.tableQueryitem.startTime||!this.tableQueryitem.endTime){
			this.jsonApiService.message('起始或结束时间不能为空');
			return 
		}
		this.censusData={
			totalWeight: '',
			num: '',
			maxWeight: '',
			minWeight: ''
		}; //统计数据
		modal.show();
		let data = {
			deviceId: this.tableQueryitem.deviceId,
			startTime:this.tableQueryitem.startTime,
			endTime:this.tableQueryitem.endTime,
			type: '3' //1单次重量明细  2单次重量明细(RFID) 3单桶数据汇总
		}
		// let reqdata = 'gridPager='+JSON.stringify(data);
		this.jsonApiService.http_obj("garbage/garbageStatistics.html", data).subscribe(data => {
			if(data.success){
				this.censusData={
					totalWeight: data.data.totalWeight,
					num: data.data.num,
					maxWeight: data.data.maxWeight,
					minWeight: data.data.minWeight
				}; //统计数据
				
			}
		})
	}
	
}